package axdoc.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.simple.SimpleJdbcInsert;
import org.springframework.stereotype.Repository;

import axdoc.model.tags.TagsContent;
import axdoc.model.tags.TagsDisease;
import axdoc.model.tags.TagsDiseaseDept;
import axdoc.model.tags.TagsDiseaseIllness;
import axdoc.model.tags.TagsIllness;
import axdoc.model.tags.TagsIllnessSymptom;
import axdoc.model.tags.TagsSymptom;

@Repository
public class TagsDao {

	@Autowired
	JdbcTemplate jdbcTemplate;

	public int addAct(TagsContent tc) {
		String sql = "insert into tags_content_word(lexicon_id,lexicon,qa_type) values(?,?,?)";
		return jdbcTemplate.update(sql,
				new Object[] { tc.getLexiconId(), tc.getLexicon().trim(), tc.getQaType().trim() });
	}

	public int addDisease(TagsDisease tc) {
		SimpleJdbcInsert simpleJdbcInsert = new SimpleJdbcInsert(jdbcTemplate);
		simpleJdbcInsert.setTableName("tags_disease");
		simpleJdbcInsert.setGeneratedKeyName("disease_id");
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("disease_name", tc.getDiseaseName().trim());
		return simpleJdbcInsert.executeAndReturnKey(param).intValue();
	}

	public TagsDisease queryDisease(String diseaseName) {
		String sql = "select * from tags_disease where disease_name=? ";
		try {
			return jdbcTemplate.queryForObject(sql, new TagsDiseaseMapper(), diseaseName);
		} catch (Exception e) {
			return null;
		}
	}

	public int addDiseaseDept(TagsDiseaseDept tc) {
		SimpleJdbcInsert simpleJdbcInsert = new SimpleJdbcInsert(jdbcTemplate);
		simpleJdbcInsert.setTableName("tags_disease_dept");
		simpleJdbcInsert.setGeneratedKeyName("id");
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("disease_id", tc.getDiseaseId());
		param.put("dept", tc.getDept().trim());
		return simpleJdbcInsert.executeAndReturnKey(param).intValue();
	}

	public int addDiseaseIllness(TagsDiseaseIllness tc) {
		SimpleJdbcInsert simpleJdbcInsert = new SimpleJdbcInsert(jdbcTemplate);
		simpleJdbcInsert.setTableName("tags_disease_illness");
		simpleJdbcInsert.setGeneratedKeyName("id");
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("disease_id", tc.getDiseaseId());
		param.put("illness_id", tc.getIllnessId());
		return simpleJdbcInsert.executeAndReturnKey(param).intValue();
	}

	public int addIllness(TagsIllness tc) {
		SimpleJdbcInsert simpleJdbcInsert = new SimpleJdbcInsert(jdbcTemplate);
		simpleJdbcInsert.setTableName("tags_illness");
		simpleJdbcInsert.setGeneratedKeyName("illness_id");
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("illness_name", tc.getIllnessName().trim());
		param.put("illness_lexid", tc.getIllnessLexid());
		param.put("illness_desc", tc.getIllnessDesc()==null?"":tc.getIllnessDesc().trim());
		return simpleJdbcInsert.executeAndReturnKey(param).intValue();
	}

	public int addIllnessSymptom(TagsIllnessSymptom tc) {
		SimpleJdbcInsert simpleJdbcInsert = new SimpleJdbcInsert(jdbcTemplate);
		simpleJdbcInsert.setTableName("tags_illness_symptom");
		simpleJdbcInsert.setGeneratedKeyName("id");
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("illness_id", tc.getIllnessId());
		param.put("symptom_id", tc.getSymptomId());
		return simpleJdbcInsert.executeAndReturnKey(param).intValue();
	}

	public int addSymptom(TagsSymptom tc) {
		SimpleJdbcInsert simpleJdbcInsert = new SimpleJdbcInsert(jdbcTemplate);
		simpleJdbcInsert.setTableName("tags_symptom");
		simpleJdbcInsert.setGeneratedKeyName("symptom_id");
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("symptom_name", tc.getSymptomName().trim());
		param.put("symptom_lexid", tc.getSymptomLexid());
		return simpleJdbcInsert.executeAndReturnKey(param).intValue();
	}
}

class TagsDiseaseMapper implements RowMapper<TagsDisease> {

	@Override
	public TagsDisease mapRow(ResultSet rs, int rowNum) throws SQLException {
		TagsDisease td = new TagsDisease();
		td.setDiseaseId(rs.getInt("disease_id"));
		td.setDiseaseName(rs.getString("disease_name"));
		return td;
	}

}